grammar PL;

@members {
  int nodeCount = 0;
  String newNode(String label) {
    String nodeId = "n" + nodeCount;
    nodeCount ++;
    System.out.println(nodeId + " [label=\"" + label + "\"];");
    return nodeId;
  }
  void connect(String n1, String n2) {
    System.out.println(n1 + " -> " + n2 + ";");
  }
}

start 
@init {
  System.out.println("digraph G {");
}
  : expr EOF {System.out.println("}");}
  ;
  
expr returns [String nodeId]
  : '(' {$nodeId = this.newNode("expr");}
    OP {
          String opId = this.newNode($OP.text);
          this.connect($nodeId, opId);
       }
    (e=expr {
      this.connect($nodeId, $e.nodeId);
    })*
    ')'
  | NUM {
    $nodeId = this.newNode($NUM.text);
  }
  ;
 
NUM: ('0' .. '9')+;
OP : '+' | '*' | '-' | '/' | 'repeat' | 'print' | 'range';
WHITESPACE: (' ' | '\t' | '\n') {skip();};